import { observable, action } from 'mobx';
import { Bubble,Tools,Modal,Toast } from 'weaver-mobile'
import { SearchAdvanced, ListViewPage } from 'weaver-mobile-page';
import * as Api from '../../api/approval';

const getLabel = Tools.getLabel;
const alert = Modal.alert;

export default class ApprovalStore {
  @observable visible = false;

  @observable themeSearch = new SearchAdvanced.Store({ route: '/approval', id: 'search' }); //主题审批-高级搜索
  @observable themeList = new ListViewPage.Store({ route: '/approval', id: 'theme' }); //主题审批
  @observable showThemeCheck = false; //主题审批-批量操作
  @observable themeAdValue = ''; //高级搜索值受控
  @observable themeLoading = true;

  @observable contentSearch = new SearchAdvanced.Store({ route: '/approval', id: 'content' }); //内容审批-高级搜索
  @observable contentList = new ListViewPage.Store({ route: '/approval', id: 'content' }); //内容审批
  @observable showContentCheck = false; //内容审批-批量操作
  @observable contentAdValue = ''; //高级搜索值受控
	@observable contentLoading = true;
	@observable currentModel = {}; //当前对话框弹窗

  @action setStoreState = (params) => {
    Object.keys(params).forEach(key => {
      this[key] = params[key];
    })
  }

  /*************************************主题审批***********************/
  // 主题审批 -- 内容
  @action getThemeData = () => {
    const themeSearchParams = this.themeSearch.form.formParams || {};
    const params = {
      from: 'cowork',
      fromPage: 'itemApproval',
      layout: 2,
      jointype: 5,
      ...themeSearchParams,
    };
    Api.getCoworkList(params).then((result) => {
      const dataKey = result.sessionkey;
      if (dataKey) {
        this.dataKey = dataKey;
        this.themeList.getData({ dataKey }, () => this.themeLoading = false, true); // ture是否刷新用
      }
    })
  }

  // 主题审批 -- 高级搜索
  @action getThemeSearchCondition = () => {
    this.themeSearch.getData({
      url: '/api/cowork/itemMonitor/getCoworkItemApproveShareCondition',
      method: 'GET',
    })
  }

  // 主题审批 -- 批准、批量批准
  @action doThemeApprove = (params) => {
    let dotype = params.dotype;
    let coworkIds = params.coworkIds;
    if(dotype === undefined ){

    }else{
      if(coworkIds === ""){
        Toast.info(getLabel(390692, '请选择要批准的主题')+'!', 1);
        return;
      }
    }
    this.currentModel = alert(getLabel('500754','确定批准选定的信息吗？'),'',[
      { text: getLabel(31129, '取消') },
      {
        text: getLabel(33703, '确认'),
        onPress: () => {
          Api.batchCoworkItemApprove(params).then(result => {
            this.doThemeReset();
            this.doThemeCancel();
            Bubble.success(getLabel(30700, '操作成功')+'!', 1);
          })
        }
      }
    ])
  }

  // 主题审批 -- 删除、批量删除
  @action doThemeDelete = (params) => {
  
    let dotype = params.dotype;
    let coworkIds = params.coworkIds;
    if(dotype === undefined ){

    }else{
      if(coworkIds === ""){
        Toast.info(getLabel(390691, '请选择要删除的主题')+'!', 1);
        return;
      }
    }
    this.currentModel = alert(getLabel('500753','确认要删除选定的信息吗？') ,'', [
      { text: getLabel(31129, '取消') },
      {
        text: getLabel(33703, '确认'),
        onPress: () => {
          Api.batchCoworkItemMonitorDel(params).then(result => {
            this.doThemeReset();
            this.doThemeCancel();
            Bubble.success(getLabel(30700, '操作成功')+'!', 1);
          })
        }
      },
    ])
  }

  // 主题审批 -- 高级搜索内外name统一
  @action updateThemeAdvance = (value) => {
    this.themeAdValue = value;
    this.themeSearch.form.updateField('name', value);
  }

  // 主题审批 -- 取消
  @action doThemeCancel = () => {
    this.showThemeCheck = false;
    this.doThemeselectNone();
  }

  // 主题审批 -- 全选
  @action doThemeSelectAll = () => this.themeList.selectAll();

  // 主题审批 -- 取消全选
  @action doThemeselectNone = () => this.themeList.selectNone();

  // 主题审批 -- 高级搜素 -- 重置
  @action doThemeReset = () => {
    this.themeSearch.form.resetFields();
    this.themeAdValue = '';
    this.getThemeData();
  }

  // 主题审批 -- 组件挂载时处理
  @action doThemeMount = () => {
    this.themeSearch.form.resetFields();
    this.themeAdValue = '';
  }

  /*************************************内容审批***********************/
  // 内容审批 -- 内容
  @action getContentData = () => {
    const params = this.contentSearch.form.formParams || {};
    Api.getCoworkDiscussApprovalList(params).then((result) => {
      const dataKey = result.sessionkey;
      if (dataKey) {
        this.dataKey = dataKey;
        this.contentList.getData({ dataKey }, () => this.contentLoading = false, true); // ture是否刷新用
      }
    })
  }

  // 内容审批 -- 高级搜索
  @action getContentSearchCondition = () => {
    this.contentSearch.getData({
      url: '/api/cowork/discussApproval/getCoworkDiscussApprovalShareCondition',
      method: 'GET',
    })
  }

  // 内容审批 -- 批准、批量审批
  @action doContentApprove = (params) => {
    let dotype = params.dotype;
    let discussIds = params.discussIds;
    if(dotype === undefined ){

    }else{
      if(discussIds === ""){
        Toast.info(getLabel(390690, '请选择要批准的内容')+'!', 1);
        return;
      }
    }
    this.currentModel = alert(getLabel('500757','确定审批选定的信息吗?'),'', [
      { text: getLabel(31129, '取消') },
      {
        text: getLabel(33703, '确认'),
        onPress: () => {
          Api.batchDiscussApprove(params).then(result => {
            this.doContentReset();
            this.doContentCancel();
            Bubble.success(getLabel(30700, '操作成功')+'!', 1);
          })
        }
      },
    ])
  }

  // 内容审批 -- 删除、批量删除
  @action doContentDelete = (params) => {
    let dotype = params.dotype;
    let discussIds = params.discussIds;
    if(dotype === undefined ){

    }else{
      if(discussIds === ""){
        Toast.info(getLabel(390685, '请选择要删除的内容')+'!', 1);
        return;
      }
    }
    this.currentModel = alert(getLabel('16344','确定删除选定的信息吗?') ,'', [
      { text: getLabel(31129, '取消') },
      {
        text: getLabel(33703, '确认'),
        onPress: () => {
          Api.batchDiscussDel(params).then(result => {
            this.doContentReset();
            this.doContentCancel();
            Bubble.success(getLabel(30700, '操作成功')+'!', 1);
          })
        }
      },
    ])
  }

  // 内容审批 -- 高级搜索内外name统一
  @action updateContentAdvance = (value) => {
    this.contentAdValue = value;
    this.contentSearch.form.updateField('remark', value);
  }

  // 内容审批 -- 取消
  @action doContentCancel = () => {
    this.showContentCheck = false;
    this.doContentselectNone();
  }

  // 内容审批 -- 全选
  @action doContentSelectAll = () => this.contentList.selectAll();

  // 内容审批 -- 取消全选
  @action doContentselectNone = () => this.contentList.selectNone();

  // 内容审批 -- 高级搜素 -- 重置
  @action doContentReset = () => {
    this.contentSearch.form.resetFields();
    this.contentAdValue = '';
    this.getContentData();
  }

  // 内容审批 -- 组件挂载时处理
  @action doContentMount = () => {
    this.contentSearch.form.resetFields();
    this.contentAdValue = '';
	}
	
	//关闭所有弹窗
	@action closeAllModel = () => {
		this.currentModel && typeof this.currentModel.close === 'function' && this.currentModel.close();
	}
}